package com.xs.ahoi.hcxs.ui.dialog;

import android.content.Context;
import android.content.DialogInterface;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.OnClick;
import com.xs.ahoi.hcxs.R;
import com.xs.ahoi.hcxs.dao.ConfigDao;
import com.xs.ahoi.hcxs.service.BookFactory;
import com.xs.ahoi.hcxs.utils.tts.TtsUtil;
import com.zhy.autolayout.AutoRelativeLayout;

/**
 * author: cdfuhuichao
 * date: 2019/4/28 17:18
 * path: com.xs.ahoi.hcxs.ui.dialog.DialogTtsSetting
 * description: tts语音合成设置
 */
public class DialogTtsSetting extends BaseDialog {

    @BindView(R.id.tvSpeedAdd)
    public TextView tvSpeedAdd;
    @BindView(R.id.tvSpeedSub)
    public TextView tvSpeedSub;
    @BindView(R.id.tvSpeedText)
    public TextView tvSpeedText;
    @BindView(R.id.tvTonesAdd)
    public TextView tvTonesAdd;
    @BindView(R.id.tvTonesSub)
    public TextView tvTonesSub;
    @BindView(R.id.tvTonesText)
    public TextView tvTonesText;
    @BindView(R.id.speedBarTotal)
    public AutoRelativeLayout speedBarTotal;
    @BindView(R.id.speedBarProcess)
    public TextView speedBarProcess;
    @BindView(R.id.tonesBarTotal)
    public AutoRelativeLayout tonesBarTotal;
    @BindView(R.id.tonesBarProcess)
    public TextView tonesBarProcess;

    // tts语速
    private int ttsSpeed;
    // tts语调
    private int ttsTones;

    // 最大的语速和语调值
    private static final int total = 15;

    public DialogTtsSetting(@NonNull Context context) {
        super(context, R.style.common_dialog);
    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    @Override
    public int getLayoutRes() {
        return R.layout.dialog_tts_setting;
    }

    @Override
    protected int showPosition() {
        return Gravity.CENTER;
    }

    @Override
    protected void initData() {

        // tts语速
        ttsSpeed = ConfigDao.getInstance().getTtsSpeed();
        // tts语调
        ttsTones = ConfigDao.getInstance().getTtsTones();

        this.setOnShowListener(new OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                // 设置语速显示文字
                updateTtsParamText();
            }
        });
    }

    @OnClick({R.id.tvDialogOk, R.id.tvDialogCancel,
            R.id.tvSpeedAdd, R.id.tvSpeedSub,
            R.id.tvTonesAdd, R.id.tvTonesSub})
    public void onClick(View v) {
        switch (v.getId()) {
            // 确认
            case R.id.tvDialogOk:
                // 更新tts语速
                changeTtsParam();
                super.dismiss();
                break;
            // 取消
            case R.id.tvDialogCancel:
                super.dismiss();
                break;
            // 语速+
            case R.id.tvSpeedAdd:
                if (ttsSpeed < total) {
                    ttsSpeed++;
                    updateTtsParamText();
                }
                break;
            // 语速-
            case R.id.tvSpeedSub:
                if (ttsSpeed > 0) {
                    ttsSpeed--;
                    updateTtsParamText();
                }
                break;
            // 语调+
            case R.id.tvTonesAdd:
                if (ttsTones < total) {
                    ttsTones++;
                    updateTtsParamText();
                }
                break;
            // 语调-
            case R.id.tvTonesSub:
                if (ttsTones > 0) {
                    ttsTones--;
                    updateTtsParamText();
                }
                break;
        }
    }

    private void changeTtsParam() {
        // 更新config
        ConfigDao.getInstance().setTtsSpeed(ttsSpeed);
        ConfigDao.getInstance().setTtsTones(ttsTones);

        // 更新tts
        TtsUtil.getInstance().changeTtsParam();
        if (BookFactory.getInstance().isPlaying()) {
            TtsUtil.getInstance().stop();
            TtsUtil.getInstance().start();
        }
    }

    // 设置语速显示文字
    private void updateTtsParamText() {
        tvSpeedText.setText(ttsSpeed + " / " + total);
        tvTonesText.setText(ttsTones + " / " + total);
        updateProcessWidth(ttsSpeed, ttsTones);
    }

    // 更新进度条
    private void updateProcessWidth(int speedPercent, int tonesPercent) {
        int totalWidth = speedBarTotal.getMeasuredWidth();
        ViewGroup.LayoutParams params = speedBarProcess.getLayoutParams();
        params.width = Double.valueOf(totalWidth * (speedPercent / (total * 1.0))).intValue();
        speedBarProcess.setLayoutParams(params);

        totalWidth = tonesBarTotal.getMeasuredWidth();
        params = tonesBarProcess.getLayoutParams();
        params.width = Double.valueOf(totalWidth * (tonesPercent / (total * 1.0))).intValue();
        tonesBarProcess.setLayoutParams(params);
    }
}
